package com.dgut.legalservice.controller;



import com.dgut.legalservice.domain.User;
import com.dgut.legalservice.service.MailService;

import com.dgut.legalservice.vCode.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/center")
public class SendMailController {
    @Autowired
    private MailService mailService;

    @Value("${spring.mail.username}")
    private String from;

    @ResponseBody
    @RequestMapping("/changeEmail")
    public boolean changeEMail(HttpSession session) throws InterruptedException {  //更换邮箱--发送验证码
        RandomString randomString = new RandomString();
        String vCode = randomString.getRandomString(4);
        String to = "382134740@qq.com";   //发送到的邮箱
        String subject = "法务服务平台邮箱更改"; //title
        String content = "验证码："+vCode; //content
        Date date = new Date();  //验证码创建时间
        session.setAttribute("vCode",vCode);  //将验证码放进session
        System.out.println(session.getAttribute("vCode"));
        mailService.sendSimpleMail(to,subject,content);//发送邮件

        return true;

    }
    @ResponseBody
    @RequestMapping("/sendEmail")
    public boolean sendMail(HttpSession session,String loginName,String email) throws InterruptedException {  //更换邮箱--发送验证码
        RandomString randomString = new RandomString();
        String vCode = randomString.getRandomString(4); //随机4位验证码
        String to = email;   //发送到的邮箱
        String subject = "注册验证码"; //title
        String content = "验证码："+vCode; //content
        session.setAttribute("vCode",vCode);  //将验证码放进session
        session.setAttribute("checkName",loginName);  //将要注册的用户名放进session,验证码只能用于该用户名注册
        mailService.sendSimpleMail(to,subject,content);//发送邮件
        return true;
    }

}
